package org.doubango.FrCall.Services.Impl;

import org.doubango.FrCall.Engine;
import org.doubango.FrCall.OpenfireNetService;
import org.doubango.FrCall.R;
import org.doubango.ngn.utils.NgnConfigurationEntry;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Mode;
import org.jivesoftware.smack.packet.Presence.Type;
import org.jivesoftware.smackx.packet.VCard;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class OpenfireTask extends AsyncTask<Object, Object, Object> {
	public static final String TAG = OpenfireTask.class.getCanonicalName();
	public static final int LOGIN_TASK = 0;
	public static final int CONNECT_TASK = 1;
	public static final int LOAD_VCARD = 2;
	public static final int SAVE_VCARD = 3;
	public static final int LOGOUT_TASK = 4;
	public int doCode = -1;
	public String errorStr = "";
	public OpenfireNetService service;

	@Override
	protected void onPreExecute() {
		OpenfireNetService.HostIP = Engine.getInstance().getConfigurationService().getString(NgnConfigurationEntry.NETWORK_PCSCF_HOST, NgnConfigurationEntry.DEFAULT_NETWORK_PCSCF_HOST).trim();
	}
	@Override
	protected Object doInBackground(Object... params) {
		doCode = (Integer) params[0];
		switch (doCode) {
		case CONNECT_TASK:
			service = (OpenfireNetService) params[1];
			if(!OpenfireNetService.mConnection.isConnected())
				return connectToService(OpenfireNetService.HostIP);
			break;
		case LOGIN_TASK:
			service = (OpenfireNetService) params[1];
			String username = (String) params[2];
			String password = (String) params[3];
			Type login_status = (Type) params[4];
			Login(username, password, login_status);
			return new Boolean(OpenfireNetService.mConnection.isAuthenticated());
		case LOGOUT_TASK:
			service = (OpenfireNetService) params[1];
			return Logout();
		case LOAD_VCARD:
		{
			service = (OpenfireNetService) params[1];
			String userId = (String) params[2];
			VCard vCard = new VCard();
			try {
				vCard.load(OpenfireNetService.mConnection,userId);
			} catch (XMPPException e) {
				errorStr = e.getMessage();
				return null;
			}
			return vCard;
		}
		case SAVE_VCARD:
		{
			service = (OpenfireNetService) params[1];
			VCard vCard = (VCard) params[2];
			try {
				vCard.save(OpenfireNetService.getXMPPConnection());
			} catch (XMPPException e) {
				e.printStackTrace();
				return false;
			}
			return true;
		}	
		default:
			break;
		}
		return null;
	}
	@Override
	protected void onPostExecute(Object result) {
//		super.onPostExecute(result);
		switch (doCode) {
		case CONNECT_TASK:
		{
			if(result!=null)
			{
				(service).getListHander().sendEmptyMessage(OpenfireNetService.RESPONSE_CONNECT_TO_SERVICER);
			}
		}
			break;
		case LOGIN_TASK:
		{
			if(((Boolean) result).booleanValue())
				(service).getListHander().sendEmptyMessage(OpenfireNetService.RESPONSE_LOGIN_SUCCESS);
			else
				(service).getListHander().sendEmptyMessage(OpenfireNetService.RESPONSE_LOGIN_FAIL);
		}
			break;
		case LOAD_VCARD:
		{ 
			if(result == null)
			{
				Toast.makeText(service, errorStr, Toast.LENGTH_SHORT);
				return;
			}
			OpenfireNetService.setTempCard((VCard) result);
			String username = ((VCard)result).getJabberId();
			Bundle bundle = new Bundle();
			bundle.putString(service.getResources().getString(R.string.REQ_CARD_USER), username);
			bundle.putBoolean(service.getResources().getString(R.string.RES_CARD_RESULT), result!=null);
			Message msg = new Message();
			msg.what = OpenfireNetService.RESPONSE_LOAD_CARD;
			msg.setData(bundle);
			service.getListHander().sendMessage(msg);
		}
		break;
		case LOGOUT_TASK:
		{
			
		}
		default:
			break;
		}
	}
	
	public boolean connectToService(String hostIP) {
		if (OpenfireNetService.mConnection == null)
			return false;
		try {
			OpenfireNetService.mConnection.connect();
		} catch (XMPPException e) {
			Log.d(TAG, "Connecting::" + e.toString());
		}
		return OpenfireNetService.mConnection.isConnected();
	}
	public boolean Logout() {
		if(!OpenfireNetService.mConnection.isConnected())
			return true;
		Presence presence = new Presence(Type.unavailable);
//		OpenfireNetService.mConnection.sendPacket(presence);
		OpenfireNetService.mConnection.disconnect(presence);
		return !OpenfireNetService.mConnection.isAuthenticated();
	}
	
	public void Login(String username, String password,
			Presence.Type login_status) {
		try {
			if (!OpenfireNetService.mConnection.isConnected())
				connectToService(OpenfireNetService.HostIP);
			if (OpenfireNetService.mConnection.isAuthenticated())
				return;
			if (OpenfireNetService.mConnection.isConnected())
				OpenfireNetService.mConnection.login(username, password,"BangBangChat");

			Presence presence = new Presence(login_status);
			if (login_status == Type.unavailable)
				presence.setStatus("隐身");
			else
				presence.setStatus("在线");
			presence.setMode(Mode.available);
			if(OpenfireNetService.mConnection.isConnected())
			{
				OpenfireNetService.mConnection.sendPacket(presence);
			}
			OpenfireNetService.userId = OpenfireNetService.mConnection
					.getUser();
		} catch (XMPPException e) {
			Log.d(TAG, doCode + "::" + e.getXMPPError());
		}
	}
}
